<?php
namespace GracefulTheme\GracefulThemeBundle\Controller;

use Symfony\Component\HttpFoundation\Request;

use Topxia\AdminBundle\Controller\BaseController;
use Topxia\Common\ArrayToolkit;


class GracefulThemeAdminController extends BaseController
{
	public function saveConfigAction(Request $request)
    {
        $config = $request->request->get('config');
        $currentData = $request->request->get('currentData');

        $config = $this->getThemeService()->saveCurrentThemeConfig($config);

        if ($currentData) {
            return $this->render('GracefulThemeBundle:Theme:theme-edit-config-li.html.twig', array(
                'pendant' => $currentData
            ));
        }
        return $this->createJsonResponse(true);
    }

    public function confirmConfigAction(Request $request)
    {
        $this->getThemeService()->saveConfirmConfig();
        return $this->redirect($this->generateUrl('admin_edit_theme'));
    }

    public function resetConfigAction(Request $request)
    {
        $this->getThemeService()->resetConfig();
        return $this->redirect($this->generateUrl('admin_edit_theme'));
    }

    public function editAction (Request $request)
    {
        if (!$this->getThemeService()->isAllowedConfig()) {
            return $this->redirect($this->generateUrl('admin_setting_theme'));
        }

        $themeConfig = $this->getThemeService()->getCurrentThemeConfig();

        return $this->render('GracefulThemeBundle:Theme:edit.html.twig', array(
            'themeConfig' => $themeConfig['config'],
            'allConfig' => $themeConfig['allConfig']
        ));
    }

    public function showAction(Request $request)
    {
        $themeConfig = $this->getThemeService()->getCurrentThemeConfig();

        return $this->render('GracefulThemeBundle:Default:show.html.twig', array(
            'themeConfig' => $themeConfig['config'],
            'allConfig' => $themeConfig['allConfig']
        ));
    }

    public function themeConfigEditAction(Request $request)
    {
        $config = $request->query->get('config');

        $code = "edit" . $this->fiterCode($config['code']);

        return $this->$code($config);
    }

    private function fiterCode($code)
    {
        $codes = explode('-', $code);
        $code = '';
        foreach ($codes as $value) {
            $code .= ucfirst($value);
        }
        return $code;
    }

    private function editRecommendCourse ($config)
    {
        return $this->render('GracefulThemeBundle:Theme:edit-left-courses-modal.html.twig', array(
            'config' => $config
        ));
    }

    private function editCategoryCourse ($config)
    {
        return $this->render('GracefulThemeBundle:Theme:edit-left-courses-modal.html.twig', array(
            'config' => $config
        ));
    }

    private function editLiveCourse ($config)
    {
        return $this->render('GracefulThemeBundle:Theme:edit-left-courses-modal.html.twig', array(
            'config' => $config
        ));
    }

    private function editRecommendTopic ($config)
    {
        return $this->render('GracefulThemeBundle:Theme:edit-left-threads-modal.html.twig', array(
            'config' => $config
        ));
    }

    private function editInformation ($config)
    {
        return $this->render('GracefulThemeBundle:Theme:edit-left-articles-modal.html.twig', array(
            'config' => $config
        ));
    }

    private function editLecturers ($config)
    {
        return $this->render('GracefulThemeBundle:Theme:edit-left-teachers-modal.html.twig', array(
            'config' => $config
        ));
    }

    private function editVip ($config)
    {
        return $this->render('GracefulThemeBundle:Theme:edit-right-teacher-modal.html.twig', array(
            'config' => $config
        ));
    }

    private function editSidebarLiveCourse ($config)
    {
        return $this->render('GracefulThemeBundle:Theme:edit-right-live-courses-modal.html.twig', array(
            'config' => $config
        ));
    }

    private function editPayCourse ($config)
    {
        return $this->render('GracefulThemeBundle:Theme:edit-right-popular-courses-modal.html.twig', array(
            'config' => $config
        ));
    }

    private function editPopularTags ($config)
    {
        return $this->render('GracefulThemeBundle:Theme:edit-right-tags-modal.html.twig', array(
            'config' => $config
        ));
    }

    private function editLatestReviews ($config)
    {
        return $this->render('GracefulThemeBundle:Theme:edit-right-reviews-modal.html.twig', array(
            'config' => $config
        ));
    }

    private function editPromotedTeacherBlock ($config)
    {
        return $this->render('GracefulThemeBundle:Theme:edit-right-teacher-modal.html.twig', array(
            'config' => $config
        ));
    }

    private function editTheDynamic ($config)
    {
        return $this->render('GracefulThemeBundle:Theme:edit-right-articles-modal.html.twig', array(
            'config' => $config
        ));
    }

    private function editTheLearningDynamics ($config)
    {
        return $this->render('GracefulThemeBundle:Theme:edit-right-learns-modal.html.twig', array(
            'config' => $config
        ));
    }

    private function getThemeService()
    {
        return $this->getServiceKernel()->createService('Theme.ThemeService');
    }
}
